package cfs.model;

import java.sql.Date;
import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import cfs.databean.CFSTransaction;
import cfs.helper.TransactionTypes;

public class TransactionDAO extends GenericDAO<CFSTransaction> {

	private BeanFactory<CFSTransaction> factory;

	public TransactionDAO(GenericDAO<?>[] referencedBeanDAOs) {
		super(CFSTransaction.class, "transaction",
				new String[] { "transaction_id" }, referencedBeanDAOs);
		setUseAutoIncrementOnCreate(true);

		BeanTable<CFSTransaction> trasactionTable = getTable();
		trasactionTable.setIdleConnectionCleanup(true);
		factory = trasactionTable.getFactory();

	}

	public Date getLastTradingDay(int customer_id) throws DAOException {
		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.equals("customer_id", customer_id),
					MatchArg.max("execute_date"));
			if (list.length == 0)
				return null;
			else
				return list[0].getExecute_date();
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public List<CFSTransaction> getTransactionListByCustomer(int customerId)
			throws DAOException {

		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.equals("customer_id", customerId));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			if (Transaction.isActive())
				Transaction.rollback();
			throw new DAOException(e);
		}
	}

	public List<CFSTransaction> getPendingTransactionListByCustomer(
			int customerId, String type) throws DAOException {

		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.equals("customer_id", customerId),
					MatchArg.equals("status", type));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			if (Transaction.isActive())
				Transaction.rollback();
			throw new DAOException(e);
		}
	}

	public List<CFSTransaction> getPendingTransaction(String type)
			throws DAOException {

		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.equals("status", type));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			if (Transaction.isActive())
				Transaction.rollback();
			throw new DAOException(e);
		}
	}

	public synchronized CFSTransaction[] getTransactions(int customerID)
			throws DAOException {
		try {
			CFSTransaction[] list = factory.match(MatchArg.equals(
					"customer_id", customerID));

			return list;
		} catch (RollbackException e) {
			e.printStackTrace();
			throw new DAOException(e);
		}
	}

	public List<CFSTransaction> getTransactionList(int customer_id, int type)
			throws DAOException {
		try {
			CFSTransaction[] list = factory.match(
					MatchArg.equals("customer_id", customer_id),
					MatchArg.equals("transaction_type", type));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public Date getLastTradingDay() throws DAOException {
		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.max("execute_date"));
			if (list.length == 0)
				return null;
			else
				return list[0].getExecute_date();
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public List<CFSTransaction> getAllTransactions() throws DAOException {
		try {
			CFSTransaction[] list = getFactory().match(
					MatchArg.notEquals("transaction_type",
							TransactionTypes.DUMMY));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
        
        public List<CFSTransaction> getFundTransactionList(int fund_id, int customer_id, int type)
			throws DAOException {
		try {
			CFSTransaction[] list = factory.match(
                                        MatchArg.equals("fund_id", fund_id),
					MatchArg.equals("customer_id", customer_id),
					MatchArg.equals("transaction_type", type),
                                        MatchArg.equals("status", "Executed"));
			return Arrays.asList(list);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
}
